package org.jeecgframework.web.service.impl;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.CbsQuickRepairServiceI;
import org.jeecgframework.web.service.WxCommonService;
import org.jeecgframework.web.system.pojo.base.CbsQuickRepairEntity;
import org.jeecgframework.web.system.pojo.base.CbsQuickRepairPicEntity;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.pojo.base.ZyEmailEntity;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.SendMailUtil;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import antlr.StringUtils;

@Service("cbsQuickRepairService")
@Transactional
public class CbsQuickRepairServiceImpl extends CommonServiceImpl implements CbsQuickRepairServiceI {

	@Autowired
	SystemService systemService;
	@Autowired
	WxCommonService wxCommonService;
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((CbsQuickRepairEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((CbsQuickRepairEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((CbsQuickRepairEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(CbsQuickRepairEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(CbsQuickRepairEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(CbsQuickRepairEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,CbsQuickRepairEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{cinema_name}",String.valueOf(t.getCinemaName()));
 		sql  = sql.replace("#{es_sn}",String.valueOf(t.getEsSn()));
 		sql  = sql.replace("#{product_model}",String.valueOf(t.getProductModel()));
 		sql  = sql.replace("#{fault_code}",String.valueOf(t.getFaultCode()));
 		sql  = sql.replace("#{fault_detail}",String.valueOf(t.getFaultDetail()));
 		sql  = sql.replace("#{contacts_name}",String.valueOf(t.getContactsName()));
 		sql  = sql.replace("#{contacts_phone}",String.valueOf(t.getContactsPhone()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public void saveFaultPics(CbsQuickRepairEntity cbsQuickRepair, String faultPic) {
//		cbsQuickRepair.setOrderNo(wxCommonService.generateServiceId(WxCommonService.ServiceEnum.KSBX));
		//保存实体 
		super.save(cbsQuickRepair);
		String quickRepairId = cbsQuickRepair.getId();
		Map<String, Object> templeteMap = new HashMap<String, Object>();
		templeteMap.put("page", cbsQuickRepair);
		//保存图片关联
		List<File> files = new ArrayList<File>();
		if(StringUtil.isNotEmpty(faultPic)){
			String[] faultPicArr = faultPic.split(",");
			List<CbsQuickRepairPicEntity> ts = new ArrayList<CbsQuickRepairPicEntity>();
			for (String fileLogId : faultPicArr) {
				ZyFileLogEntity fileLog = get(ZyFileLogEntity.class, fileLogId);
				if(fileLog != null){
					File file = new File(fileLog.getFilePath());
					if(file.exists()){
						files.add(file);
					}
				}
				CbsQuickRepairPicEntity t = new CbsQuickRepairPicEntity();
				t.setFileLogId(fileLogId);
				t.setQuickRepairId(quickRepairId);
				ts.add(t);
			}
			super.batchSave(ts);
		}
		String templeteName = "mail_quick_repair.ftl";
		//发送附件邮件
		List<TSType> mailType = findHql("from TSType where TSTypegroup.typegroupcode=?", StaticValue.DIC_SERVICE_MAILADDRESS);
		String subject = StaticValue.DIC_SERVICE_MAIL_PREFIX+"-快速报修-"+cbsQuickRepair.getOrderNo()+"-"+cbsQuickRepair.getCinemaName();
        for (TSType tsType : mailType) {
            if(StringUtil.isNotEmpty(tsType.getTypename())){
                if(files.size() > 0){
                	SendMailUtil.sendEMail(tsType.getTypename(),subject,templeteName,templeteMap, files, systemService);
                }else{
                	SendMailUtil.sendEMail(tsType.getTypename(),subject,templeteName,templeteMap, null, systemService);
                }
            }
        }
        try {
        	//单独给 JV-helpdesk@barco.com 发
        	ZyEmailEntity email = new ZyEmailEntity();
        	email.setEmailReceiver("dl_bjv_helpdesk@cfg-barco.com");
        	email.setEmailSubject(subject);
        	SendMailUtil.sendFtlMail(email,templeteName,templeteMap,systemService);
        	email = new ZyEmailEntity();
        	email.setEmailReceiver("wx@cfg-barco.com");
        	email.setEmailSubject(subject);
        	SendMailUtil.sendFtlMail(email,templeteName,templeteMap,systemService);
		} catch (Exception e) {
		}
	}

	@Override
	public List<CbsQuickRepairEntity> quickRepairOrderList(
			HttpServletRequest request) {
		String phone = request.getParameter("phone");
		if(StringUtil.isEmpty(phone) && null != ResourceUtil.getSessionUserName()){
			phone = ResourceUtil.getSessionUserName().getMobilePhone();
		}
		return findHql("from CbsQuickRepairEntity where contactsPhone=? order by createDate desc", oConvertUtils.getString(phone));
	}

}